using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace zevenseas.Community.Web.UI.WebControls
{
    public class SPBoundUserField : SPBoundField
    {
        protected override void ChildControlDataBinding(Control childControl, object dataItem, MemberDescriptor dataFieldPropertyDescriptor)
        {
            Literal fieldValue = new Literal();

            string propertyValue = GetPropertyValue(dataItem, dataFieldPropertyDescriptor.Name);

            SPFieldUserValue gridUserValue = new SPFieldUserValue(SPContext.Current.Web, propertyValue);
            if (!string.IsNullOrEmpty(gridUserValue.ToString()))
            {
                fieldValue.Text = gridUserValue.User.Name.ToString();
            }

            PlaceHolder placeHolder = (PlaceHolder)childControl;
            placeHolder.Controls.Add(fieldValue);
        }

        protected override Control GetChildControlInstance()
        {
            return new PlaceHolder();
        }
    }
}